﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.EntityClient;
using CongregacaoOs.Data;

namespace CongregacaoOs.Business
{
    [System.ComponentModel.DataObject]
    public class ccIgrejaBus
    {
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public bool IgrejaSaveOrUpdate(ccIgreja igreja)
        {

            using (EntityConnection conn = new EntityConnection("name = CcbOrdemServico"))
            {
                try
                {
                    using (EntityCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "CcbOrdemServico.ccIgrejaSave";
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;

                        EntityParameter param;

                        param = cmd.Parameters.Add("IDIGREJA", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (igreja.idIgreja != 0)
                            param.Value = igreja.idIgreja;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("NOME", DbType.String);
                        param.Direction = ParameterDirection.Input;
                        if (igreja.nome != "")
                            param.Value = igreja.nome;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("NUMERO", DbType.String);
                        param.Direction = ParameterDirection.Input;
                        if (igreja.numero != "")
                            param.Value = igreja.numero;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("IDCIDADE", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (igreja.idCidade != 0)
                            param.Value = igreja.idCidade;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("CEP", DbType.String);
                        param.Direction = ParameterDirection.Input;
                        if (igreja.cep != "")
                            param.Value = igreja.cep;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("ENDERECO", DbType.String);
                        param.Direction = ParameterDirection.Input;
                        if (igreja.endereco != "")
                            param.Value = igreja.endereco;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("TELEFONE", DbType.String);
                        param.Direction = ParameterDirection.Input;
                        if (igreja.telefone != "")
                            param.Value = igreja.telefone;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("IDREGIONAL", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (igreja.idRegional != 0)
                            param.Value = igreja.idRegional;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("IDDISTRITAL", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (igreja.idDistrital != 0)
                            param.Value = igreja.idDistrital;
                        else
                            param.Value = DBNull.Value;


                        using (conn) 
                        {
                            if (conn.State == ConnectionState.Closed) conn.Open();
                                cmd.ExecuteNonQuery();
                                return true;
                        }
                                                                                              
                    }
                }
                catch (Exception)
                {
                    return false;
                }

            }
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public List<ccIgrejaSelect_Result> IgrejaSelect(ccIgreja igreja)
        {
            using (EntityConnection conn = new EntityConnection("name = CcbOrdemServico"))
            {
                
                try
                {
                    using (EntityCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "CcbOrdemServico.ccIgrejaSelect1";
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;

                        EntityParameter param;

                        param = cmd.Parameters.Add("IDIGREJA", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (igreja.idIgreja != 0)
                            param.Value = igreja.idIgreja;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("NOME", DbType.String);
                        param.Direction = ParameterDirection.Input;
                        if (igreja.nome != "")
                            param.Value = igreja.nome;
                        else
                            param.Value = DBNull.Value;


                        param = cmd.Parameters.Add("IDREGIONAL", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (igreja.idRegional != 0)
                            param.Value = igreja.idRegional;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("IDDISTRITAL", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (igreja.idDistrital != 0)
                            param.Value = igreja.idDistrital;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("IDCIDADE", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (igreja.idCidade != 0)
                            param.Value = igreja.idCidade;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("NUMERO", DbType.String);
                        param.Direction = ParameterDirection.Input;
                        if (igreja.numero != "")
                            param.Value = igreja.numero;
                        else
                            param.Value = DBNull.Value;



                        using (conn)
                        {
                            if(conn.State == ConnectionState.Closed) conn.Open();

                            using (EntityDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
                            {

                                List<ccIgrejaSelect_Result> _result = new List<ccIgrejaSelect_Result>();
                                while (reader.Read())
                                {
                                    ccIgrejaSelect_Result _igreja = new ccIgrejaSelect_Result();
                                    _igreja.idIgreja = reader.IsDBNull(reader.GetOrdinal("idIgreja")) == false ? reader.GetInt32(reader.GetOrdinal("idIgreja")) : 0;
                                    _igreja.Igreja = reader.IsDBNull(reader.GetOrdinal("Igreja")) == false ? reader.GetString(reader.GetOrdinal("Igreja")) : "";
                                    _igreja.cep = reader.IsDBNull(reader.GetOrdinal("cep")) == false ? reader.GetString(reader.GetOrdinal("cep")) : "";
                                    _igreja.endereco = reader.IsDBNull(reader.GetOrdinal("endereco")) == false ? reader.GetString(reader.GetOrdinal("endereco")) : "";                                    
                                    _igreja.numero = reader.IsDBNull(reader.GetOrdinal("numero")) == false ? reader.GetString(reader.GetOrdinal("numero")) : "";
                                    _igreja.telefone = reader.IsDBNull(reader.GetOrdinal("telefone")) == false ? reader.GetString(reader.GetOrdinal("telefone")) : "";
                                    _igreja.regional = reader.IsDBNull(reader.GetOrdinal("regional")) == false ? reader.GetString(reader.GetOrdinal("regional")) : "";
                                    _igreja.distrital = reader.IsDBNull(reader.GetOrdinal("distrital")) == false ? reader.GetString(reader.GetOrdinal("distrital")) : "";
                                    _igreja.cidade = reader.IsDBNull(reader.GetOrdinal("cidade")) == false ? reader.GetString(reader.GetOrdinal("cidade")) : "";

                                    _result.Add(_igreja);
                                }
                                return _result;
                            }
                        }


                    }
                }
                catch (Exception)
                {
                    return null;
                }

            }
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public void IgrejaDelete(int id)
        {
            try
            {
                using (CcbOrdemServico objDB = new CcbOrdemServico())
                {
                    objDB.ccIgrejaDelete(id);
                }
            }
            catch (System.Exception)
            {

                throw;
            }
        }
    }
}
